<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.org/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Title</title>
    <link rel="stylesheet" href="/resources/layui/css/layui.css">
</head>
<body>
<script src="/resources/layui/layui.js"></script>

<script th:inline="none">
    //一般直接写在一个js文件中
    layui.use(['layer', 'form','table'], function(){
        var layer = layui.layer
            ,form = layui.form
            ,table = layui.table
            ,$=layui.jquery;
        //引入表格，使用表格
        var roleTable=table.render({
            elem:'#roleTable',
            id:'roleListTable',
            height:600,
            url:'/role/roleTable',
            page:true,
            limits:[5,10,15,20],
            limit:5,
            cols:[[
                {field:'id',title:'编号',width:80,type:'checkbox'},
                {field: 'name', title: '角色'},
                {title: '操作', width:250,templet:'#roleListBar',fixed:"right",align:"center"}
            ]]
        })
        //权限列表
        table.on("tool(rolesList)",function (obj) {
            var data=obj.data;
            var id=data.id;
            switch(obj.event){
                case 'grantRole':
                    console.log("333333")
                    var permissIndex=layer.open({
                        title:'权限分配',
                        type:2,
                        content:'/gorolePermiss',
                        area:['300px','500px'],
                        btn:['确定','取消'],
                        success:function (layero,index) {
                            var body=layer.getChildFrame('body',index);
                            body.find("#roleId").val(data.id);
                        },
                        yes:function (index,layero) {
                            var res=window["layui-layer-iframe"+index].callbackdata();
                            $.ajax({
                                data:res,
                                url:'/permission/assignRight',
                                traditional:true,
                                success:function (regs) {
                                    console.log(res)
                                    layer.msg(regs.message);
                                    layer.close(permissIndex);
                                }
                            })
                        },
                        btn2:function (index,layero) {
                            
                        }
                    })
                    break;
                case 'del':
                    layer.confirm('确定删除角色',{icon:3,title:'提示信息'},function (del) {
                        $.ajax({
                            type:'post',
                            url:'/role/dellRole'+id,
                            success:function () {
                                layer.close(del)
                                roleTable.reload()
                            }
                        })
                    })
                    break;
                case 'edit':
                    var updaterole=layer.open({
                        type:2,
                        title:'修改角色',
                        area:['400px','200px'],
                        content:'/updrole',
                        success:function (layero,index) {
                            var body=layer.getChildFrame('body',updaterole)
                            console.log(data)
                            body.find("#name").val(data.name)
                            body.find("#roleid").val(data.id)
                        }
                    })

            }
        })
        //搜素功能
        $(".searchBtn").click(function () {
            var name=$("#name").val();
            table.reload("roleListTable",{url:'/role/roleTable?name='+name})
        })
        //批量删除
        $("#removeRole").click(function () {
            //获取复选框
            var check=table.checkStatus("roleListTable")
            var data=check.data;
            //创建一个数组
            var ids=new Array();
            if(data.length==0){
                layer.msg("至少选择一个")
            }else {
                for(var i=0;i<data.length;i++){
                    ids.push(data[i].id)
                }
                layer.confirm('确定删除角色',{icon:3,title:'提示信息'},function (delAllindex) {
                    $.ajax({
                        type:'post',
                        url:'/role/deleteAllRolePath',
                        data:{ids:ids},
                        traditional:true,
                        success:function () {
                            layer.close(delAllindex)
                            roleTable.reload()
                        }
                    })
                })
            }
        })
        //增加角色
        $("#addPatch").click(function () {
            var roleadd= layer.open({
                type:2,
                title:'增加角色',
                area:['400px','200px'],
                content:'/addrole',
                success:function (layero,index) {

                },
            })
        })
    });
</script>

<blockquote class="layui-elem-quote quoteBox">
    <form class="layui-form" action="">
        <div class="layui-inline">
            <input type="text" class="layui-input" id="name" placeholder="请输入角色">
        </div>

        <div class="layui-inline">
            <button type="button" class="layui-btn searchBtn">
                搜索
            </button>
        </div>
        <div class="layui-inline">
            <shiro:hasPermission name="user:delectall">
                <button type="button" class="layui-btn" id="removeRole">
                    批量删除
                </button>
            </shiro:hasPermission>

        </div>
        <div class="layui-inline">
            <shiro:hasPermission name="user:add">
                <button type="button" class="layui-btn" id="addPatch">
                    增加
                </button>
            </shiro:hasPermission>
        </div>
    </form>
</blockquote>

<table id="roleTable" lay-filter="rolesList">
    <!--操作-->
    <script id="roleListBar" type="text/html">
        <shiro:hasPermission name="user:edit">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        </shiro:hasPermission>

        <shiro:hasPermission name="user:delete">
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
        </shiro:hasPermission>

        <shiro:hasPermission name="user:look">
            <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="look">预览</a>
        </shiro:hasPermission>

        <shiro:hasPermission name="user:grant">
            <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="grantRole">分配权限</a>
        </shiro:hasPermission>
    </script>
</table>

</body>
</html>